package com.leboncoin.html;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Goal of this class:
 * - search for any content matching classified semantic
 * - fetch content into classified model
 * @author ben
 */
public class ContentMining {
    
    public static Integer MINIMUM_HIT_NUMBER = 4;
    
    /**
     * Search texts in html source that match at least MINIMUM_HIT_NUMBER keywords
     * @param htmlSource
     * @return list of classifed text from html source
     */
    public static List<String> searchClassifieds(String htmlSource) {
        
        List<String> result = new ArrayList<String>();      
        Map<String, Integer> matchList = new HashMap<String, Integer>(); // keywords match with hit number

        Document document = Jsoup.parse(htmlSource);
        
        // Build match list
        for (String keyWord : Arrays.asList(keyWords)) {
            Pattern pattern = null; //new Pattern(keyWord, Pattern.CASE_INSENSITIVE);
            
            Elements elements = document.getElementsMatchingOwnText(keyWord);
            for (Element element : elements) {
                Integer hitNumber = matchList.get(element.ownText());
                if (hitNumber == null){
                    matchList.put(element.ownText(), 1);
                } else {
                    hitNumber++;
                }
            }
 
        }
        
        // Remove match not containing enough hits 
        for (String match : matchList.keySet()) {
            if (matchList.get(match) >= MINIMUM_HIT_NUMBER) {
                result.add(match);
            }
        }
        
        return result;
        
    }
    
    /** list of keywords related to classifieds */
    static String[] keyWords = {
        // Batiment
        "maison",
        "appartement",
        "loft",
        "grange",
        "longère",
        "ferme",
        "fermette",
        "villa",
        "pavillon",
        "t1",	"T1",	"f1",	"F1",
        "t2",	"T2",	"f2",	"F2",
        "t3",	"T3",	"f3",	"F3",
        "t4",	"T4",	"f4",	"F4",
        "t5",	"T5",	"f5",	"F5",
        "t6",	"T6",	"f6",	"F6",

        // Pieces
        "piece",
        "chambre",
        "bureau",
        "wc",
        "toilette",
        "sanitaire", 
        "cuisine",
        "salle de bain",
        "sdb",
        "garage",
        "box",
        "salle a manger",
        "salle à manger",
        "salon",
        "buanderie",
        "grenier",
        
        // Terrain
        "terrain",
        "jardin",
        "dépendance",
        "dependance",
        
        // Autres
        "chauffage",
        "electrique",        
        "électrique",
        "gaz",
        "fioul"
    };    
    
}
